#include <pthread.h> 
#include <stdio.h> 
#include <sys/time.h> 
#include <string.h> 
#include <stdlib.h>
#include <errno.h>
#include<unistd.h>
#define MAX 10

pthread_mutex_t mut;
pthread_t pthread[2];
int a = 0, i;

void *pthread0(void)
{
    for(i = 0; i < MAX; i++)
    {
    printf("thread0 : number= %d\n", a); 
    pthread_mutex_lock(&mut);
        a++;
    pthread_mutex_unlock(&mut);
    sleep(3);
    }
    pthread_exit(NULL);
}

void *pthread1(void)
{    for(i = 0; i < MAX; i++)
    {
    printf("thread1 : number= %d\n", a); 
    pthread_mutex_lock(&mut);
        a++;
    pthread_mutex_unlock(&mut);
    sleep(2);
    }
    pthread_exit(NULL);
}
void mypthread_create(void)
{
    memset(pthread, 0, sizeof(pthread));

    if(pthread_create(&pthread[0], NULL, (void *)pthread0, NULL) != 0)
    {
        printf("creat 0 error\n");
        exit(1);
    }
    else
    {
        printf("pthread0 ok\n");
    }
    if(pthread_create(&pthread[1], NULL, (void *)pthread1, NULL) != 0)
    {
        printf("creat 1 error\n");
        exit(1);
    }
    else
    {
        printf("pthread1 ok\n");
    }
}

void pthread_wait(void)
{
    if(pthread[0] != 0)
    {
        pthread_join(pthread[0], NULL);
    }
    if(pthread[1] != 0)
    {
        pthread_join(pthread[1], NULL);
    }
}

int main(void)
{
    pthread_mutex_init(&mut, NULL);
    
    mypthread_create();
    
    pthread_wait();
    
    return 0;
}